<?php
namespace Tbf\Bundle\TbfBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * 命令行password hash函数调用(生成mock数据里面的加密密码用)
 */
class PasswordVerifyCommand extends Command
{
    protected function configure()
    {
        $this
            ->setName('tbf:password_verify')
            ->setDescription('call password_verify()')
            ->addArgument('password')
            ->addArgument('hash');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        if (!function_exists('password_verify')){
            $output->writeln('you do not have function password_verify.');
            $output->writeln('Need php >= 5.5.0 or a package named "ircmaxell/password-compat".');
            return -1;
        }
        $password = $input->getArgument('password');
        if ($password===null){
            throw new \Exception('require password argument');
        }
        $hash = $input->getArgument('hash');
        if ($hash===null){
            throw new \Exception('require hash argument');
        }
        $ret = password_verify($password,$hash);
        if ($ret===true){
            $output->writeln('PASS');
            return 0;
        }
        $output->writeln('FAIL');
        return -1;
    }
}
